<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>首页</title>
<%@include file="../static/include/head.jsp"%>

<script
	src="<%=request.getContextPath()%>/static/assets/global/scripts/metronic.js"
	type="text/javascript"></script>
<script
	src="<%=request.getContextPath()%>/static/assets/admin/layout/scripts/layout.js"
	type="text/javascript"></script>
<script
	src="<%=request.getContextPath()%>/static/assets/admin/layout/scripts/quick-sidebar.js"
	type="text/javascript"></script>
<script
	src="<%=request.getContextPath()%>/static/assets/admin/layout/scripts/demo.js"
	type="text/javascript"></script>

<link
	href="<%=request.getContextPath()%>/static/assets/global/plugins/datatables/plugins/bootstrap/dataTables.bootstrap.css"
	rel="stylesheet" type="text/css" />
<script
	src="<%=request.getContextPath()%>/static/assets/global/plugins/datatables/media/js/jquery.dataTables.min.js"
	type="text/javascript"></script>
<script
	src="<%=request.getContextPath()%>/static/assets/global/plugins/datatables/plugins/bootstrap/dataTables.bootstrap.js"
	type="text/javascript"></script>

<script>
	jQuery(document).ready(function() {
		Metronic.init();
		Layout.init();
		QuickSidebar.init();
		Demo.init();
		$("#serchButton").click(function() {
			tableUserInit.fnDraw();
		});

		var tableUserInit = $("#table_user").dataTable({
				"paging" : true, //翻页功能
				"lengthChange" : true, //改变每页显示数据数量
				"searching" : false, //过滤功能
				"ordering" : true, //排序功能
				"order" : [ [ 0, "desc" ] ],
				"processing" : false,
				"serverSide" : true, //服务器数据
				"lengthMenu" : [ 10, 15, 30, 50 ],
				"pageLength" : 10,
				"language" : {
				"processing" : "努力加载中...",
				"search" : "搜索&nbsp;:",
				"lengthMenu" : "一页显示 _MENU_ 条数据",
				"info" : "当前第 _START_ 条到第 _END_ 条记录，共 _TOTAL_ 条记录",
				"loadingRecords" : "正在努力的加载数据...",
				"infoEmpty" : "当前第 0 条到第 0 条记录，共 0 条记录",
				"emptyTable" : "没有找到您查询的数据信息"
			},
			"ajax" : {
				"url" : "user/getUsersByName",
				"type" : "POST",
				"data" : function(data) {
					data.userName = $("#userName").val();
				}
			},
			"columns" : [{
					"data" : "id"
				},{
					"data" : "name"
				},{
					"data" : "alias"
				},{
					"data" : "email"
				},{
					"data" : "phone"
				},{
					"orderable": false,
					"data" : null,
					"defaultContent" : "<div style='width:120'><button type='button' onclick='lookUserInit($(this))' class='btn blue btn-xs'>查看 <i class='fa fa-edit'></i></button><button type='button' onclick='deleteUserInit($(this))' class='btn red btn-xs'>删除 <i class='fa fa-times'></i></button></div>"
				}]
		});
	});
	
	
	// 新增按钮初始化
	function addUserInit() {
		clearInput();
		$("#edit_user_btn").hide();
	    $("#submit_user_btn").show();
		$("#user_name_edit").removeAttr("readonly");
		$("#user_alias_edit").removeAttr("readonly");
		$("#user_password1_tr").show();
		$("#user_password2_tr").show();
		$("#user_password_edit").removeAttr("readonly");
		$("#user_password2_edit").removeAttr("readonly");
		$("#user_email_edit").removeAttr("readonly");
		$("#user_phone_edit").removeAttr("readonly");
		
		$("#user_name_edit").removeAttr("style");
		$("#user_alias_edit").removeAttr("style");
		$("#user_email_edit").removeAttr("style");
		$("#user_phone_edit").removeAttr("style");
		
		$("#user_name_edit").css("background-color","");
		$("#user_alias_edit").css("background-color","");
		$("#user_email_edit").css("background-color","");
		$("#user_phone_edit").css("background-color","");
		
		$("#managerUser_div").modal("show");
		$("#managerUser_title").text("新增用户");
		$("#managerUser_color").attr("class","portlet box green");
		
    };
    
 	// 查看按钮初始化
	function lookUserInit(item) {
		clearInput();
		$("#edit_user_btn").show();
	    $("#submit_user_btn").hide();
		$("#user_name_edit").attr("readonly","readonly");
		$("#user_alias_edit").attr("readonly","readonly");
		$("#user_password1_tr").hide();
		$("#user_password2_tr").hide();
		$("#user_password_edit").attr("readonly","readonly");
		$("#user_password2_edit").attr("readonly","readonly");
		$("#user_email_edit").attr("readonly","readonly");
		$("#user_phone_edit").attr("readonly","readonly");
		$("#user_principal_edit").attr("readonly","readonly");
		
		$("#user_name_edit").attr("style","border:0px");
		$("#user_name_edit").css("background-color",$("#user_name_edit").parents('td').css('background-color'));
		$("#user_alias_edit").attr("style","border:0px");
		$("#user_alias_edit").css("background-color",$("#user_alias_edit").parents('td').css('background-color'));
		$("#user_email_edit").attr("style","border:0px");
		$("#user_email_edit").css("background-color",$("#user_email_edit").parents('td').css('background-color'));
		$("#user_phone_edit").attr("style","border:0px");
		$("#user_phone_edit").css("background-color",$("#user_phone_edit").parents('td').css('background-color'));
		$("#user_principal_edit").attr("style","border:0px");
		$("#user_principal_edit").css("background-color",$("#user_phone_edit").parents('td').css('background-color'));
		
		$("#managerUser_title").text("查看用户详情");
		$("#managerUser_color").attr("class","portlet box blue");
        var tableRow = item.parents('tr')[0];
        var idTh = $(tableRow).children().get(0);
        $.post("<%=request.getContextPath()%>/user/getUsersById", 
    		{ 
    			id: $(idTh).text()
    		}, 
			function(json){
    			for(var index in json.data){
    				var val = json.data[index];
    				if(typeof($("#user_"+index+"_edit")) != "undefined"){
    					$("#user_"+index+"_edit").val(json.data[index]);
    				}
    			}
				$("#managerUser_div").modal("show");
			},"json"
    	);
    };
	
	// 修改按钮初始化
	function editUserInit() {
		$("#edit_user_btn").hide();
	    $("#submit_user_btn").show();
	    $("#user_name_edit").removeAttr("readonly");
		$("#user_alias_edit").removeAttr("readonly");
		$("#user_password1_tr").show();
		$("#user_password2_tr").show();
		$("#user_password_edit").removeAttr("readonly");
		$("#user_password2_edit").removeAttr("readonly");
		$("#user_email_edit").removeAttr("readonly");
		$("#user_phone_edit").removeAttr("readonly");
		$("#user_principal_edit").removeAttr("readonly");
		
		$("#user_name_edit").removeAttr("style");
		$("#user_alias_edit").removeAttr("style");
		$("#user_email_edit").removeAttr("style");
		$("#user_phone_edit").removeAttr("style");
		$("#user_principal_edit").removeAttr("style");
		
		$("#user_name_edit").css("background-color","");
		$("#user_alias_edit").css("background-color","");
		$("#user_email_edit").css("background-color","");
		$("#user_phone_edit").css("background-color","");
		$("#user_principal_edit").css("background-color","");
		
		$("#managerUser_title").text("修改用户资料");
		$("#managerUser_color").attr("class","portlet box yellow");
    };
    
 	// 删除按钮初始化
	function deleteUserInit(item) {
		$("#user_id_delete").val("");
        var tableRow = item.parents('tr')[0];
        var idTh = $(tableRow).children().get(0);
        var userNameTh = $(tableRow).children().get(1);
        var userAliasTh = $(tableRow).children().get(2);
        var id = $(idTh).text();
        $("#user_id_delete").val(id);
        var message = "您确定要删除编号:"+id+",登陆名:"+$(userNameTh).text()+",姓名:"+$(userAliasTh).text()+",的用户吗?(本操作不可恢复)"
        $("#deleteUser_p").text(message);
        $("#deleteUser_div").modal("show");
    };
	
	
	//简单校验用户输入
	function submit_validation(){
		var obj = new Object();
		var uname = document.getElementById("user_name_edit").value;
		if(uname==null || '' == uname){
			alert('用户名不能为空');
			return;
		}
		var pwd1 = $("#user_password_edit").val();
		if(pwd1==null || ''==pwd1){
			alert('密码不能为空');
			return;
		}
		var pwd2 = document.getElementById("user_password2_edit").value;
		if(pwd2 == null || ''==pwd2){
			alert('确认密码不能为空');
			return;
		}
		if(pwd1 != pwd2){
			alert('密码和确认密码不一致请重新输入');
			return;
		}else{
			obj.password = pwd1;
		}
		var aliasname = document.getElementById("user_alias_edit").value;
		if(aliasname == null || ''==aliasname){
			alert('用户昵称不能为空');
			return;
		}
/* 		var email = document.getElementById("user_email_edit").value;
		var phone = document.getElementById("user_phone_edit").value; */
		
		$("#userInfoTable2 input[type='text']").each(function(e){
			var stationId = $(this).attr("id");
		    var idArray = stationId.split('_');
		    obj[idArray[1]] = $(this).val();
		 });
		var id = $("#stock_id_edit").val();
		
		$("#userInfoTable2 select").each(function(e){
		    var inputId = $(this).attr("id");
		    var idArray = inputId.split('_');
		    if($(this).val()!=''){
			    obj[idArray[1]] = $(this).val();
		    }
		});
		
		$("#userInfoTable2 textarea").each(function(e){
		    var inputId = $(this).attr("id");
		    var idArray = inputId.split('_');
		    if($(this).val()!=''){
			    obj[idArray[1]] = $(this).val();
		    }
		});
		
		var id = $("#user_id_edit").val();
		if(id == ''){
			/* save_user(uname,pwd1,aliasname,email,phone) */
			save_user(obj);
		}else{
			/* update_user(id,uname,pwd1,aliasname,email,phone) */
			update_user(obj);
		}
		clearInput();
	}
	
	function save_user(obj){
		$.ajax({
			type:"POST",
			url:"<%=request.getContextPath()%>/user/add",
			data:obj,
			dataType :'json',
			timeout:"20000",
			success:function(data,textStatus){
				var msg = data.msg;
				var code = data.code;
				if(code == 0 && 'succ' == msg){
					alert("保存成功");
					$("#managerUser_div").modal("hide");
					$("#table_user").dataTable().fnDraw();
				}				
			},
			error : function(XMLResponse) {
				alert("提交超时请检查网络环境");
				$("#managerStock_div").modal("hide");
				$("#table_user").dataTable().fnDraw();
			}
		});
	}
	
	function update_user(obj){
		$.post("<%=request.getContextPath()%>/user/updateUser",obj, 
	    	function(result){
	    		if(result == -1){
					alert("系统内部错误,请与管理员联系！");
					$("#managerStock_div").modal("hide");
					$("#table_user").dataTable().fnDraw();
				}else if(result == 0){
					alert("修改账户成功！");
					$("#managerUser_div").modal("hide");
					$("#table_user").dataTable().fnDraw();
				}
			}
	    );
	}
	<%-- // 更新用户提交
	function update_user(id,uname,pwd,aliasname,email,phone){
		$.post("<%=request.getContextPath()%>/user/updateUser", 
	    	{ 
				id:id,
				username:uname,
				password:pwd,
				alias:aliasname,
				email:email,
				phone:phone
	    	}, 
	    	function(result){
	    		if(result == -1){
					alert("系统内部错误,请与管理员联系！");
					$("#managerUser_div").modal("hide");
					$("#table_user").dataTable().fnDraw();
				}else if(result == 0){
					alert("修改用户成功！");
					$("#managerUser_div").modal("hide");
					$("#table_user").dataTable().fnDraw();
				}
			}
	    );
	}
	
	
	
	// 增加用户提交
	function save_user(uname,pwd,aliasname,email,phone){
		$.ajax({
			type:"POST",
			url:"<%=request.getContextPath()%>/user/add",
			data: 
				{
					username:uname,
					password:pwd,
					user_alias:aliasname,
					user_email:email,
					user_phone:phone,
				},
			dataType :'json',
			timeout:"20000",
			success:function(data,textStatus){
				var msg = data.msg;
				var code = data.code;
				if(code == 0 && 'succ' == msg){
					alert("保存成功");
					$("#managerUser_div").modal("hide");
					$("#table_user").dataTable().fnDraw();
				}				
			},
			error : function(XMLResponse) {
				alert("提交超时请检查网络环境");
				$("#managerUser_div").modal("hide");
				$("#table_user").dataTable().fnDraw();
			}
		});
	} --%>
	
	// 删除用户提交
	function delete_user(){
		$.post("<%=request.getContextPath()%>/user/deleteUsersById", 
	    	{ 
				id:$("#user_id_delete").val()
	    	}, 
	    	function(result){
	    		if(result == -1){
					alert("系统内部错误,请与管理员联系！");
					$("#deleteUser_div").modal("hide");
					$("#user_id_delete").val("");
					$("#table_user").dataTable().fnDraw();
				}else if(result == 0){
					alert("删除用户成功！");
					$("#deleteUser_div").modal("hide");
					$("#user_id_delete").val("");
					$("#table_user").dataTable().fnDraw();
				}
			}
	    );
	}
	
	function clearInput(){
		$("#user_id_edit").val("");
		$("#user_name_edit").val("");
		$("#user_alias_edit").val("");
		$("#user_password_edit").val("");
		$("#user_password2_edit").val("");
		$("#user_email_edit").val("");
		$("#user_phone_edit").val("");
	}
</script>
</head>
<body class="page-header-fixed page-quick-sidebar-over-content">
	<%@include file="../static/include/header.jsp"%>
	<div class="clearfix"></div>
	<div class="page-container">
		<%@include file="../static/include/sidebar.jsp"%>
		<!-- BEGIN CONTENT -->
		<div class="page-content-wrapper">
			<div class="page-content">
				<!-- BEGIN STYLE CUSTOMIZER -->
				<%@include file="../static/include/styles.jsp"%>
				<!-- END STYLE CUSTOMIZER -->
				<div class="row">
					<div class="col-md-12">
						<!-- BEGIN PAGE HEADER-->
						<h3 class="page-title">
							用户管理 <small>管理您的用户</small>
						</h3>
						<div class="page-bar">
							<ul class="page-breadcrumb">
								<li>
									<i class="fa fa-home"></i>
									<a href="index">主页</a>
									<i class="fa fa-angle-right"></i>
								</li>
								<li>
									<a href="userManager">用户管理</a>
								</li>
							</ul>
						</div>
						<!-- END PAGE HEADER-->
						<!-- BEGIN ROW -->
						<div class="portlet box blue-madison">
							<div class="portlet-title">
								<div class="caption">
									<i class="fa fa-user"></i>用户
								</div>
								<div class="tools">
									<a href="javascript:;" class="collapse"></a>
								</div>
							</div>
							<div class="portlet-body">
								<div>
									<ul class="nav nav-tabs ">
										<li style="float: left">
											<button type="button" onclick="addUserInit()" class="btn green">
												新增 <i class="fa fa-plus"></i>
											</button>
										</li>
										<li style="float: right">
											<button id="serchButton" type="button" class="btn green">搜索</button>
										</li>
										<li style="float: right">
											<input id="userName" type="text" class="form-control" placeholder="请输入用户名">
										</li>
									</ul>
									<div class="tab-pane fade active in" id="tab_1_1">
										<table id="table_user"
											class="table table-striped table-hover table-bordered">
											<thead>
												<tr>
													<th>编号</th>
													<th>登陆名</th>
													<th>姓名</th>
													<th>邮箱</th>
													<th>电话</th>
													<th>操作</th>
												</tr>
											</thead>
											<tbody>
											</tbody>
										</table>
									</div>
								</div>
							</div>
						</div>
						<!-- END ROW -->
					</div>
				</div>
			</div>
			<!-- END CONTENT -->
		</div>
	</div>
	<%@include file="../static/include/footer.jsp"%>
	<!-- modal BEGIN -->
	<div id="managerUser_div" class="modal fade" tabindex="-1" aria-hidden="true">
		<div class="modal-dialog">
			<div id="managerUser_color" class="portlet box blue">
				<div class="portlet-title">
					<div class="caption">
						<i class="fa fa-user"></i>用户管理
					</div>
					<div id="managerUser_title" class="tools">
					</div>
				</div>
				<div class="portlet-body form">
					<div class="form-body">
						<table  style="margin: 3px" class="table table-striped table-hover table-bordered" id="userInfoTable2">
							<tr hidden="true">
								<td>编号</td>
								<td>
									<input class="col-md-12" id="user_id_edit" type="text" readonly="readonly"></input>
								</td>
							</tr>
							<tr>
								<td>用户名</td>
								<td>
									<input class="col-md-12" id="user_name_edit" type="text"></input>								
								</td>
							</tr>
							<tr id="user_password1_tr">
								<td>密码</td>
								<td>
									<input class="col-md-12" id="user_password_edit" type="password" onkeyup="value=value.replace(/[^\a-\z\A-\Z0-9\~\!\@\#\$\%\^\&\*\_\+\-\=\.\,]/g,'')"></input>
									&nbsp;&nbsp;<font color="red">*</font>
								</td>
							</tr>
							<tr id="user_password2_tr">
								<td>确认密码</td>
								<td>
									<input class="col-md-12" id="user_password2_edit" type="password" onkeyup="value=value.replace(/[^\a-\z\A-\Z0-9\~\!\@\#\$\%\^\&\*\_\+\-\=\.\,]/g,'')"></input>
									&nbsp;&nbsp;<font color="red">*</font>
								</td>
							</tr>
							<tr>
								<td>昵称</td>
								<td>
									<input class="col-md-12" id="user_alias_edit" type="text"></input>
								</td>
							</tr>
							<tr>
								<td>邮箱</td>
								<td>
									<input class="col-md-12" id="user_email_edit" type="text"></input>
								</td>
							</tr>
							<tr>
								<td>手机号</td>
								<td>
									<input class="col-md-12" id="user_phone_edit" type="text"></input>
								</td>
							</tr>
							<tr>
								<td>本金</td>
								<td>
									<input class="col-md-12" id="user_principal_edit" type="text"></input>
								</td>
							</tr>
							<tr>
								<td>可用现金</td>
								<td>
									<input class="col-md-12" id="user_cash_edit" type="text"></input>
								</td>
							</tr>
							<tr>
								<td>备注</td>
								<!-- <td colspan="3"><input class="col-md-12" id="stock_remark_edit" type="text"></input></td> -->
								<td><textarea rows="3" cols="20" class="col-md-12" id="user_beizhu_edit"></textarea></td>
							</tr>
						</table>
					</div>
					<div class="form-actions right">
						<button type="button" data-dismiss="modal" class="btn default">返回</button>
						<button id="edit_user_btn" type="button" onclick="editUserInit()" class="btn yellow" hidden="true">修改</button>
						<button id="submit_user_btn" type="button" onclick="submit_validation()" class="btn green">提交</button>
					</div>		
				</div>
			</div>
		</div>
	</div>
	<!-- modal END -->
	
	<!-- modal BEGIN -->
	<div id="deleteUser_div" class="modal fade" tabindex="-1" aria-hidden="true">
		<div class="modal-dialog">
			<div class="portlet box red">
				<div class="portlet-title">
					<div class="caption">
						<i class="fa fa-user"></i>用户管理
					</div>
					<div class="tools">
						删除用户
					</div>
				</div>
				<div class="portlet-body form">
					<div class="form-body">
						<input class="col-md-10" id="user_id_delete" type="text" readonly="readonly" hidden="true"></input>
						<p id="deleteUser_p"></p>
					</div>
					<div class="form-actions right">
						<button type="button" data-dismiss="modal" class="btn default">返回</button>
						<button type="button" onclick="delete_user()" class="btn red">删除</button>
					</div>		
				</div>
			</div>
		</div>
	</div>
	<!-- modal END -->
</body>
</html>
